package com.util;

import org.springframework.stereotype.Component;

@Component
public class PositionTool {
    public final static double a = 6378245.0;  
    public final static double ee = 0.00669342162296594323;  
  
    // 判断坐标是否在中国  
    public static boolean outOfChina(double[] bdLocation) {  
        double lat = bdLocation[1];  
        double lon = bdLocation[0];
        if (lon < 72.004 || lon > 137.8347)  
            return true;  
        if (lat < 0.8293 || lat > 55.8271)  
            return true;  
        if ((119.962 < lon && lon < 121.750) && (21.586 < lat && lat < 25.463))  
            return true;  
  
        return false;  
    }  
  
    public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
  
    public static double[] BAIDU_to_WGS84(double[] bdLocation) {  
        if (outOfChina(bdLocation)) {  
            return bdLocation;  
        }  
        double x = bdLocation[0] - 0.0065;  
        double y = bdLocation[1] - 0.006;  
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);  
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);  
        bdLocation[0]=z * Math.cos(theta);  
        bdLocation[1]=z * Math.sin(theta);  
        return GCJ02_to_WGS84(bdLocation);  
    }  
  
    public static double[] GCJ02_to_WGS84(double[] bdLocation) {  
        if (outOfChina(bdLocation)) {  
            return bdLocation;  
        }  
        double[] tmpLocation = new double[2];  
        tmpLocation[1]=bdLocation[1];  
        tmpLocation[0]=bdLocation[0];  
        double[] tmpLatLng = WGS84_to_GCJ02(tmpLocation);  
        double tmpLat = 2 * bdLocation[1] - tmpLatLng[1];  
        double tmpLng = 2 * bdLocation[0] - tmpLatLng[0];  
        for (int i = 0; i < 0; ++i) {  
            tmpLocation[1]=bdLocation[1];  
            tmpLocation[0]=bdLocation[0];  
            tmpLatLng = WGS84_to_GCJ02(tmpLocation);  
            tmpLat = 2 * tmpLat - tmpLatLng[1];  
            tmpLng = 2 * tmpLng - tmpLatLng[0];  
        }  
        bdLocation[1]=tmpLat;  
        bdLocation[0]=tmpLng;  
        return bdLocation;  
    }  
  
    public static double[] WGS84_to_GCJ02(double[] bdLocation) {  
        if (outOfChina(bdLocation)) {  
            return bdLocation;  
        }  
        double dLat = transformLat(bdLocation[0] - 105.0,  
                bdLocation[1] - 35.0);  
        double dLon = transformLon(bdLocation[0] - 105.0,  
                bdLocation[1] - 35.0);  
        double radLat = bdLocation[1] / 180.0 * Math.PI;  
        double magic = Math.sin(radLat);  
        magic = 1 - ee * magic * magic;  
        double sqrtMagic = Math.sqrt(magic);  
        dLat = (dLat * 180.0)  
                / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);  
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);  
        bdLocation[1]=bdLocation[1] + dLat;  
        bdLocation[0]=bdLocation[0] + dLon;  
        return bdLocation;  
    }  
  
    public static double transformLat(double x, double y) {  
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y  
                + 0.2 * Math.sqrt(Math.abs(x));  
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x  
                * Math.PI)) * 2.0 / 3.0;  
        ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0  
                * Math.PI)) * 2.0 / 3.0;  
        ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y  
                * Math.PI / 30.0)) * 2.0 / 3.0;  
        return ret;  
    }  
  
    public static double transformLon(double x, double y) {  
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1  
                * Math.sqrt(Math.abs(x));  
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x  
                * Math.PI)) * 2.0 / 3.0;  
        ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0  
                * Math.PI)) * 2.0 / 3.0;  
        ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x  
                / 30.0 * Math.PI)) * 2.0 / 3.0;  
        return ret;  
    }  
}  
